package com.ruoyi.secondvitim.controller;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.secondvitim.domain.*;
import com.ruoyi.secondvitim.service.IQueFormDetailService;
import com.ruoyi.secondvitim.service.IQueFormService;
import com.ruoyi.secondvitim.service.IQueSubmitFormDetailService;
import com.ruoyi.secondvitim.service.IQueSubmitFormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/secondVitim/api")
public class ApiIndexController {

    @Autowired
    private IQueFormService queFormService;

    @Autowired
    private IQueFormDetailService  queFormDetailService;

    @Autowired
    private IQueSubmitFormService queSubmitFormService;

    @Autowired
    private IQueSubmitFormDetailService queSubmitFormDetailService;

    /**
     * 查看问卷调查列表详情
     */
    @GetMapping("/getQueForm")
    @ResponseBody
    public AjaxResult getQueForm(Long id)
    {
        QueForm queForm = queFormService.selectQueFormById(id);
        QueFormDetail queFormDetail = new QueFormDetail();
        queFormDetail.setFormId(id);
        queFormDetail.setStatus("0");
        List<QueFormDetail> detailList = queFormDetailService.selectQueFormDetailList(queFormDetail);
        queForm.setQueFormDetailList(detailList);
        return AjaxResult.success(queForm);
    }

    /**
     * 提交问卷调查表
     * @param queSubmitForm 提交问卷内容
     * */
    @Log(title = "提交量表", businessType = BusinessType.OTHER)
    @PostMapping("/submitFormQuestion")
    public AjaxResult submitFormQuestion(@RequestBody QueSubmitForm queSubmitForm){
        List<QueSubmitForm> submitForms = queSubmitFormService.selectQueSubmitFormList(queSubmitForm);
        if(submitForms.size() > 0) {
            return AjaxResult.error("已提交调查问卷不允许重复提交");
        }
        //获取表单类型
        QueForm queForm = queFormService.selectQueFormById(queSubmitForm.getFormId());
        //设置表单类型
        queSubmitForm.setType(queForm.getFormType());
        queSubmitForm.setTitle(queForm.getTitle());
        //计算问卷分数
        queSubmitFormService.insertQueSubmitForm(queSubmitForm);
        //计算结果
        SubmitFormResult submitFormResult = queSubmitFormService.calSubmitResult(queSubmitForm, queSubmitForm.getSubmitFormDetailList());
        //根据选项更新计算结果
        queSubmitForm.setTotal(submitFormResult.getTotal());
        queSubmitFormService.updateQueSubmitForm(queSubmitForm);
        List<QueSubmitFormDetail> queSubmitFormDetailList = submitFormResult.getQueSubmitFormDetailList();
        //批量插入提交的结果
        for (QueSubmitFormDetail queSubmitFormDetail : queSubmitFormDetailList) {
            queSubmitFormDetail.setSubmitFormId(queSubmitForm.getId());
            queSubmitFormDetailService.insertQueSubmitFormDetail(queSubmitFormDetail);
        }

        return AjaxResult.success("提交成功");
    }
}
